﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using Hopthoai;
using QUANLYCHAMCONG.BL.BUS;
using DgvFilterPopup;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;

namespace QUANLYCHAMCONG.PL.CMS.Nhanvien
{
    public partial class DangkyNghiPhep : UserControl
    {
        public delegate void _Close();
        public event _Close Close;
        private string _LoaiLink;
        private int _Flag;
        public int Flag
        {
            get { return _Flag; }
            set { _Flag = value; }
        }
        public string LoaiLink
        {
            get { return _LoaiLink; }
            set { _LoaiLink = value; }
        }
        STAFF nv;
        string MAPB;
        string MACV;
        int flag;
        frmHoidap f;
        TANGCA tc;
        ClsCT_TANGCACollection ds;
        CT_TANGCA ct;
        public DangkyNghiPhep()
        {
            InitializeComponent();
        }
        int FormatMonth(string month)
        {
            int thang=0;
            switch (month)
            {
                case "January":
                    thang= 1;
                    break;
                case "February":
                    thang= 2;
                    break;
                case "March":
                    thang= 3;
                    break;
                case "April":
                    thang= 4;
                    break;
                case "May":
                    thang= 5;
                    break;
                case "June":
                    thang= 6;
                    break;
                case "July":
                    thang= 7;
                    break;
                case "August":
                    thang= 8;
                    break;
                case "September":
                    thang= 9;
                    break;
                case "October":
                    thang= 10;
                    break;
                case "November":
                    thang= 11;
                    break;
                case "December":
                    thang= 12;
                    break;
            }
            return thang;
        }
        void ChangeType(string month, string year)
        {
            switch (month)
            { 
                case "January":
                    AddDay(1, year);
                    break;
                case "February":
                    AddDay(2, year);
                    break;
                case "March":
                    AddDay(3, year);
                    break;
                case "April":
                    AddDay(4, year);
                    break;
                case "May":
                    AddDay(5, year);
                    break;
                case "June":
                    AddDay(6, year);
                    break;
                case "July":
                    AddDay(7, year);
                    break;
                case "August":
                    AddDay(8, year);
                    break;
                case "September":
                    AddDay(9, year);
                    break;
                case "October":
                    AddDay(10, year);
                    break;
                case "November":
                    AddDay(11, year);
                    break;
                case "December":
                    AddDay(12, year);
                    break;
            }
        }
        void AddDay(int Month,string year)
        { 
            int y=int.Parse(year);
            switch (Month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    {
                        AddDataToCombobox(31);
                        break;
                    }
                case 2:
                    {
                        if (DateTime.IsLeapYear(y))
                            AddDataToCombobox(29);
                        else
                            AddDataToCombobox(28);
                        break;
                    }
                case 4:
                case 6:
                case 11:
                case 9:
                    {
                        AddDataToCombobox(30);
                        break;
                    }
            }
        }
        void AddDataToCombobox(int month)
        {
            string[] thang = new string[month];
            for (int i = 0; i <month; i++)
            {
                thang[i] = (i+1).ToString();
            }
            this.cmbngay.Properties.Items.Clear();
            this.cmbngay.Properties.DataSource = thang;

        }
        private void DangkyNghiPhep_Load(object sender, EventArgs e)
        {
            SetLink();
            Event();
            txtnam.Text = DateTime.Now.Year.ToString();
        }
        private void cmbthang_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (this.cmbthang.SelectedItem.ToString() != null)
            {
                ChangeType(this.cmbthang.SelectedItem.ToString(), txtnam.Text);
                this.cmbngay.Enabled = true;
            }
        }
        void Event()
        {
            this.toolbar1.Add += new Toolbar._Add(toolbar1_Add);
            this.toolbar1.Save += new Toolbar._Save(toolbar1_Save);
            this.toolbar1.Delete += new Toolbar._Delete(toolbar1_Delete);
            LoadData();
        }
        void toolbar1_Delete()
        {
            flag = 3;
            this.toolbar1.SetAdd_Edit_Delete();
            this.dgvdanhsach.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgvdanhsach.Rows[0].Selected = true;
            this.dgvdanhsach.ReadOnly = false;
            this.dgvdanhsach.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
            AddCheckbox(TANGCABUS.SelectAllPhepNam());
        }
        private void InitGrid()
        {
            DgvFilterManager f = new DgvFilterManager();
            f.ColumnFilterAdding += new ColumnFilterEventHandler(f_ColumnFilterAdding);
            f.DataGridView = (DataGridView)this.dgvdanhsach;
        }
        void f_ColumnFilterAdding(object sender, ColumnFilterEventArgs e)
        {
            switch (e.Column.Name)
            {
                case "MATC":
                case "TENNV":
                case "TENLH":
                    e.ColumnFilter = new DgvComboBoxColumnFilter();
                    break;
            }
        }
        DateTime MegeDate(int year, int month, int day)
        {
            DateTime dt = DateTime.Parse(day.ToString() + "/" + month.ToString() + "/" + year.ToString());
            return dt;
        }
        void toolbar1_Save()
        {
            bool IsExists = true;
            if (Flag == 3&&flag==1)//neu la tang ca
            {
                tc = new TANGCA();
                SHILF ca =new SHILF();
                ca.MACA=this.cmbca.SelectedValue.ToString();
                DataTable dm = new DataTable();
                dm = SHILFBUS.TongCong(ca);
                if (dm.Rows.Count != 0)
                {
                    foreach (DataRow d in dm.Rows)
                    {
                        tc.TONGCONG = float.Parse(d["CONG"].ToString());
                        tc.TONGIO = float.Parse(d["TONGGIO"].ToString()) / 60;
                        break;
                    }
                }
                tc.MATC = txtmadk.Text;
                tc.MALH = cmbloaidk.SelectedValue.ToString();
                ds = new ClsCT_TANGCACollection();
                int[] a = new int[this.cmbngay.Properties.Items.Count];
                int k = 0;
                foreach (CheckedListBoxItem j in this.cmbngay.Properties.Items)
                {
                    if (j.CheckState == CheckState.Checked)
                    {
                        a[k] = int.Parse(j.Value.ToString());
                        k++;
                    }
                }
                int[] b = new int[k];
                for (int n = 0; n < b.Length; n++)
                {
                    b[n] = a[n];
                }
                    for (int q = 0; q < b.Length; q++)
                    {
                        DateTime ngay = MegeDate(int.Parse(txtnam.Text), FormatMonth(this.cmbthang.SelectedItem.ToString()), a[q]);
                        foreach (CheckedListBoxItem i in this.cbnv.Properties.Items)
                        {
                            if (i.CheckState == CheckState.Checked)
                            {
                                ct = new CT_TANGCA();
                                ct.MANV = i.Value.ToString();
                                ct.MATC = txtmadk.Text;
                                ct.NGAYTHUCHIEN = ngay;
                                if (flag == 1)
                                {
                                    DataTable dt = new DataTable();
                                    CheckAviable c = new CheckAviable();
                                    c.MALH = cmbloaidk.SelectedValue.ToString();
                                    c.MANV = i.Value.ToString();
                                    c.NGAYTHUCHIEN = ngay;
                                    dt = CheckAviableBUS.Check(c);
                                    if (dt.Rows.Count != 0)
                                    {
                                        f = new frmHoidap("Nhân viên " + c.MANV.ToString() + " đă đăng ký về trễ !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                        f.ShowDialog();
                                        IsExists = false;
                                        continue;
                                    }
                                }
                                ds.Add(ct);
                            }

                        }
                    }
            }
            if (flag == 1 && IsExists)//truong hop them moi
            {
                bool kq = true;
                if (TANGCABUS.Insert(tc) == true)//thanh cong
                {
                    //insert chi tiet 
                    foreach (CT_TANGCA ct in ds)
                    {
                        kq = CT_TANGCABUS.Insert(ct);
                    }
                    if (kq)
                    {
                        f = new frmHoidap("Thêm phép năm thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                        f.ShowDialog();
                    }
                    else
                    {
                        TANGCABUS.Delete(tc);
                        f = new frmHoidap("Những nhân viên này đã được đăng ký phép năm trong ngày rồi!!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                        this.toolbar1.SetSave();
                    }


                }
                else
                {
                    f = new frmHoidap("Thêm phép năm thất bại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                    f.ShowDialog();
                    this.toolbar1.SetSave();
                }
                LoadData();
                Clear();

            }
            if (flag == 3)
            {
                bool kq = true;
                f = new frmHoidap("Bạn có chắc muốn xóa phép năm vừa chọn không?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                if (f.ShowDialog() == DialogResult.Yes)
                {
                    try
                    {

                        foreach (DataGridViewRow dr in this.dgvdanhsach.Rows)
                        {
                            if (dr.Cells[0].Value != null)
                            {
                                ct = new CT_TANGCA();
                                ct.MATC = dr.Cells["MATC"].Value.ToString();
                                ct.MANV = dr.Cells["MANV"].Value.ToString();
                                ct.NGAYTHUCHIEN=DateTime.Parse(dr.Cells["NGAYTHUCHIEN"].Value.ToString());
                                kq = CT_TANGCABUS.Delete(ct);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        f = new frmHoidap("Có lỗi " + ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                    }
                    if (kq)
                    {
                        f = new frmHoidap("Xóa thành công phép năm!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                        f.ShowDialog();
                        RemoveCheckbox();
                        LoadData();
                        this.dgvdanhsach.ReadOnly = true;
                        this.dgvdanhsach.AllowUserToAddRows = true;
                        Clear();
                    }

                }
            }
            if (IsExists == false)
            {
                LoadData();
                Clear();
                this.toolbar1.SetSave();
            }
        }
        void Clear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Panel)
                {
                    foreach (Control d in c.Controls)
                    {
                        if (d is TextBox)
                            d.Text = "";
                        if (d is System.Windows.Forms.ComboBox)
                            d.Enabled = false;
                        if (d is CheckedComboBoxEdit)
                        {
                            d.Enabled = false;
                            d.Text = "";
                        }
                    }
                }
            }
        }
        void toolbar1_Add()
        {
            this.txtmadk.Text = TANGCABUS.GetID();
            this.toolbar1.SetAdd_Edit_Delete();
            this.cmbloaidk.Enabled = true;
            flag = 1;
            txtnam.Text = DateTime.Now.Year.ToString();
            this.cmbca.Enabled = true;
        }
        private int FindAviable()
        {
            foreach (DataGridViewColumn dr in this.dgvdanhsach.Columns)
            {
                if (dr is DataGridViewCheckBoxColumn)
                    return 1;
            }
            return 0;
        }
        private void RemoveCheckbox()
        {
            if (FindAviable() == 1)
            {
                this.dgvdanhsach.Columns.RemoveAt(0);
                this.dgvdanhsach.Controls.RemoveByKey(((CheckBox)this.dgvdanhsach.Controls.Find("checkboxHeader", true)[0]).Name);
                LoadData();
            }
        }
        private void AddCheckbox(DataTable dt)
        {
            if (FindAviable() == 0)
            {
                DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
                cb.Width = 30;
                cb.FillWeight = 30;
                cb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.dgvdanhsach.Columns.Insert(0, cb);
                Rectangle rect = this.dgvdanhsach.GetCellDisplayRectangle(0, -1, true);
                rect.X = rect.Location.X + cb.Width / 10;
                CheckBox checkboxHeader = new CheckBox();
                checkboxHeader.Name = "checkboxHeader";
                checkboxHeader.Size = new Size(18, 18);
                checkboxHeader.Location = rect.Location;
                checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
                this.dgvdanhsach.Controls.Add(checkboxHeader);
                this.dgvdanhsach.Columns[0].Width = 50;
                this.dgvdanhsach.DataSource = dt;
            }
        }
        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)this.dgvdanhsach.Controls.Find("checkboxHeader", true)[0]).Checked == true)
            {
                for (int i = 0; i < this.dgvdanhsach.RowCount; i++)
                {
                    this.dgvdanhsach[0, i].Value = ((CheckBox)this.dgvdanhsach.Controls.Find("checkboxHeader", true)[0]).Checked;
                }
            }
            else
            {
                for (int i = 0; i < this.dgvdanhsach.RowCount; i++)
                {
                    this.dgvdanhsach[0, i].Value = false;
                }
            }
        }
        public void LoadData()
        {
            LoadCombox();
            this.dgvdanhsach.DataSource= TANGCABUS.SelectAllPhepNam();
            this.dgvdanhsach.Columns["MANV"].Visible = false;
            InitGrid();
        }
        public void LoadCombox()
        {
            this.cmbpb.DataSource = PHONGBANBUS.SelectAll().DefaultView;
            this.cmbpb.DisplayMember = "TENPB";
            this.cmbpb.ValueMember = "MAPB";
            this.cmbcv.DataSource = CHUCVUBUS.SelectAll().DefaultView;
            this.cmbcv.DisplayMember = "TENCV";
            this.cmbcv.ValueMember = "MACV";
            this.toolbar1.SetToolBar(true, false, true, false, false, false, false, false);
            this.cmbloaidk.DataSource = LOAIHINHBUS.SelectAll();
            this.cmbloaidk.DisplayMember = "TENLH";
            this.cmbloaidk.ValueMember = "MALH";
            if (this.Flag == 3)
            {
                this.cmbloaidk.SelectedIndex = this.cmbloaidk.FindString("Nghỉ phép năm");
            }
            this.cmbca.DataSource = SHILFBUS.SelectAll();
            this.cmbca.DisplayMember = "TENCA";
            this.cmbca.ValueMember = "MACA";
        }
        void Nhanvien()
        {
            nv = new STAFF();
            nv.MACV = this.MACV;
            nv.MAPB = this.MAPB;
            this.cbnv.Properties.DataSource = STAFFBUS.SelectbyGroupPB_CV(nv);
            this.cbnv.Properties.DisplayMember = "TENNV";
            this.cbnv.Properties.ValueMember = "MANV";

        }
        public void SetLink()
        {
            this.links1.SetValues("Nhân viên", LoaiLink);
        }

        private void cmbpb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbpb.SelectedValue.ToString() != "System.Data.DataRowView" && cmbpb.SelectedValue.ToString() != "")
            {
                MAPB = this.cmbpb.SelectedValue.ToString();
                cmbcv.Enabled = true;
                if (cmbcv.Enabled == true)
                {
                    Nhanvien();
                }
            }
        }

        private void cmbcv_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbcv.SelectedValue.ToString() != "System.Data.DataRowView" && cmbcv.SelectedValue.ToString() != "")
            {
                MACV = this.cmbcv.SelectedValue.ToString();
                cbnv.Enabled = true;
                Nhanvien();
            }
        }

        private void cmbloaidk_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbloaidk.SelectedValue.ToString() != "System.Data.DataRowView" && cmbloaidk.SelectedValue.ToString() != "")
            {
                cmbpb.Enabled = true;
            }
        }

        private void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }

    }
}
